package com.iotechn.unimall.admin.api.article;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.biz.service.goods.GoodsBizService;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.CmsArticleDO;
import com.iotechn.unimall.data.domain.CmsArticleTypeDO;
import com.iotechn.unimall.data.mapper.CmsArticleMapper;
import com.iotechn.unimall.data.model.Page;
import jodd.util.StringUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class CmsArticleServiceImpl implements CmsArticleService {
    @Autowired
    private CmsArticleMapper cmsArticleMapper;

    @Autowired
    private GoodsBizService goodsBizService;

     @Override
        public Page<CmsArticleDO> list(Integer page, Integer limit, String title, Long adminId) throws ServiceException {
            Wrapper<CmsArticleDO> wrapper = new EntityWrapper<CmsArticleDO>();
            /**if (!StringUtils.isEmpty(name)) {
                wrapper.like("username", name);
            }**/
            if (!StringUtils.isEmpty(title)) {
                 wrapper.like("title", title);
            }
            wrapper.orderBy("id", true);
            Integer count = cmsArticleMapper.selectCount(wrapper);
            List<CmsArticleDO> CmsArticleDOS = cmsArticleMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
            return new Page<>(CmsArticleDOS, page, limit, count);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public CmsArticleDO create(CmsArticleDO cmsArticleDO, Long adminId) throws ServiceException {
            cmsArticleDO.setAdminId(adminId);
            Date now=new Date();
            cmsArticleDO.setGmtCreate(now);
            cmsArticleDO.setGmtUpdate(now);
            if (cmsArticleMapper.insert(cmsArticleDO) > 0) {
                cmsArticleDO.setId(cmsArticleDO.getId());
                return cmsArticleDO;
            }
             throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public String update(CmsArticleDO cmsArticleDO, Long adminId) throws ServiceException {
            Long id = cmsArticleDO.getId();
            if (id == null) {
               return "fail";
            }
            Date now=new Date();
            cmsArticleDO.setGmtUpdate(now);
            if (cmsArticleMapper.updateById(cmsArticleDO) > 0) {
                return "ok";
            }
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (cmsArticleMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids)) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        for (Long spuId : ids) {
            cmsArticleMapper.deleteById(spuId);
        }
        return "ok";
    }

    @Override
    public List<CmsArticleDO> searchSpu(String title, Long adminId) throws ServiceException {
        return cmsArticleMapper.search(title);
    }

    @Override
    public CmsArticleDO detail(Long id, Long adminId) throws ServiceException {
       return cmsArticleMapper.selectById(id);
    }

    @Override
    public Boolean updateStatus(Long adminId, Long id,Integer status) throws ServiceException {
        if(status == null || (status != 0 && status != 1)){
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        CmsArticleDO cmsArticleDO = new CmsArticleDO();
        cmsArticleDO.setId(id);
        cmsArticleDO.setGmtUpdate(new Date());
        if(cmsArticleMapper.updateById(cmsArticleDO) > 0){
            return true;
        }
        return false;
    }

    @Override
    public String addLabel(CmsArticleDO cmsArticleDO, String label, Long adminId) throws ServiceException {
        Long id = cmsArticleDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now=new Date();
        if (StringUtil.isBlank(cmsArticleDO.getLabel())){
            cmsArticleDO.setLabel(label);
        }else {
            cmsArticleDO.setLabel(","+label);
        }
        cmsArticleDO.setGmtUpdate(now);
        if (cmsArticleMapper.updateById(cmsArticleDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public List<CmsArticleTypeDO> typeList(Long adminId) throws ServiceException {
        return cmsArticleMapper.typeList();
    }

}